import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_demo/controller/countController.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 将Getx中的控制器加入共享实例
  final CountController _countController = Get.put(CountController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("首页"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('首页'),
              const SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      _countController.decrease();
                    },
                    child: const Text('减少'),
                  ),
                  const SizedBox(width: 20),
                  // Obx(() => Text(_countController.count.toString())),
                  GetBuilder<CountController>(
                    builder: (controller) => Text(controller.count.toString()),
                  ),
                  const SizedBox(width: 20),
                  ElevatedButton(
                    onPressed: () {
                      _countController.increase();
                    },
                    child: const Text('增加'),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  // 跳转到列表页
                  // Get.toNamed('/list');
                  // 跳转到列表页，并传递参数
                  Get.toNamed('/list', arguments: {'name': '张三'});
                },
                child: const Text('去列表页'),
              )
            ],
          ),
        ));
  }
}
